//
//  AJTTStoryController.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import UIKit

class AJTTStoryController: AJTTBaseViewController {
   
   @IBOutlet var tableView: UITableView!
    lazy var viewModel: AJTTStoryViewModel = {
        return AJTTStoryViewModel()
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wx_navigationBar.isHidden = true
        setTab()
        // Do any additional setup after loading the view.
    }
    @IBAction func tapSearchAction(_ sender: Any) {
        self.view.endEditing(true)
        self.loadData()
    }
    @IBAction func tapAddStoryAction(_ sender: Any) {
        self.navigationController?.pushViewController(AJTTAddStoryController(), animated: true)
    }
    
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "AJTTStoryListCell", bundle: .main), forCellReuseIdentifier: "AJTTStoryListCell")
    }
    func loadData(){
        self.viewModel.getPageData() { models in
            print(self.viewModel.lately)
            self.tableView.reloadData()
        }
        
    }
}
extension AJTTStoryController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.viewModel.dataSource[indexPath.row]
        let cell:AJTTStoryListCell = tableView.dequeueReusableCell(withIdentifier: "AJTTStoryListCell") as! AJTTStoryListCell
        cell.model = model
        cell.bgView.backgroundColor = indexPath.row % 2 == 0 ? #colorLiteral(red: 0.7437943816, green: 0.8530778289, blue: 1, alpha: 1) : #colorLiteral(red: 0.9484501481, green: 0.8433007002, blue: 0.4145256579, alpha: 1)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailsVC = AJTTStoryDetailsController()
        detailsVC.model = self.viewModel.dataSource[indexPath.row]
        self.navigationController?.pushViewController(detailsVC, animated: true)
    }
}
